package vn.com.vega.music.utils;

import java.lang.ref.WeakReference;

import android.os.AsyncTask;
import vn.com.vega.chacha.v2.ActivityBase;
import vn.com.vega.music.clientserver.JsonAlbum;
import vn.com.vega.music.clientserver.JsonArtist;
import vn.com.vega.music.objects.Artist;
import vn.com.vega.music.objects.Artist;

public class ArttistUtil {
	public interface LikeAndUnLikeArtistListener {
		public void startDoLike(Artist artist);
		public void likeSuccess(Artist artist);
		public void likeFail(Artist artist, String errorMsg);
		
		public void startDoUnlike(Artist artist);
		public void unlikeSuccess(Artist artist);
		public void unlikeFail(Artist artist, String errorMsg);
	}
	
	static class LikeAndUnlikeArtist extends AsyncTask<Void, Void, Void> {
		WeakReference<ActivityBase> activityBaseRfc;
		Artist artist;
		LikeAndUnLikeArtistListener likeAndUnlikeArtistListener;
		boolean doLike;
		String errorMessage;
		
		public LikeAndUnlikeArtist(ActivityBase act, Artist album, LikeAndUnLikeArtistListener listener, boolean doLile) {
			activityBaseRfc = new WeakReference<ActivityBase>(act);
			this.artist = album;
			likeAndUnlikeArtistListener = listener;
			this.doLike = doLile;
		}
		
		private boolean checkIsLive() {
			ActivityBase act = activityBaseRfc.get();
			if (act == null || act.isDestroy) {
				return false;
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return null;
			}
			if (artist == null) {
				errorMessage = "";
				return null;
			}
			JsonArtist jsonArtist = JsonArtist.follow(artist.id);
			if (jsonArtist.isSuccess()) {
				errorMessage = null;
				return null;
			} else {
				errorMessage = jsonArtist.getErrorMessage();
				if (errorMessage == null) {
					errorMessage = "";
				}
			}
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (!checkIsLive() || isCancelled()) {
				return;
			}
			if (likeAndUnlikeArtistListener != null) {
				if (doLike) {
					if (errorMessage == null) {
						likeAndUnlikeArtistListener.likeSuccess(artist);
					} else {
						likeAndUnlikeArtistListener.likeFail(artist, errorMessage);
					}
				} else {
					if (errorMessage == null) {
						likeAndUnlikeArtistListener.unlikeSuccess(artist);
					} else {
						likeAndUnlikeArtistListener.unlikeFail(artist, errorMessage);
					} 
				}
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return;
			}
			if (likeAndUnlikeArtistListener != null) {
				if (doLike) {
					likeAndUnlikeArtistListener.startDoLike(artist);
				} else {
					likeAndUnlikeArtistListener.startDoUnlike(artist);
				}
			}
		}
	}
	
	public static void likeArtist(ActivityBase act, Artist artist, LikeAndUnLikeArtistListener likeAlbumListener) {
		LikeAndUnlikeArtist likeAndUnlikeArtist = new LikeAndUnlikeArtist(act, artist, likeAlbumListener, true);
		likeAndUnlikeArtist.execute();
	}
	
	public static void unLikeAlbum(ActivityBase act, Artist artist, LikeAndUnLikeArtistListener likeAlbumListener) {
		LikeAndUnlikeArtist likeAndUnlikeArtist = new LikeAndUnlikeArtist(act, artist, likeAlbumListener, false);
		likeAndUnlikeArtist.execute();
	}
}
